package com.unifiedlibrary.amall_page.base.recycler;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xzk
 * @data 2019/5/7
 * @email xiezhengkun@beyondsoft.com
 * @remark recyclerView的基类适配器
 */
public abstract class RcvBaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    public Context context;
    private int checkPoi = -1;
    private int layoutId;
    private List<T> dataList = new ArrayList<>();
    protected ItemOnClickListener itemOnClickListener;
    protected ItemOnLongClickListener itemOnLongClickListener;

    public int getCheckPoi() {
        return checkPoi;
    }

    public void setCheckPoi(int checkPoi) {
        this.checkPoi = checkPoi;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setItemOnLongClickListener(ItemOnLongClickListener itemOnLongClickListener) {
        this.itemOnLongClickListener = itemOnLongClickListener;
    }

    public interface ItemOnClickListener<T> {

        void onItem(T t, int position);
    }

    public interface ItemOnLongClickListener<T> {

        void onLongItem(T t, int position);
    }

    public RcvBaseAdapter(Context context, int layoutId) {
        this.context = context;
        this.layoutId = layoutId;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(context, itemView);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        bindViewHolder(holder, dataList.get(position), position);
        if (itemOnClickListener != null) {
            holder.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemOnClickListener.onItem(dataList.get(position), position);

                }
            } );
        }
        if (itemOnLongClickListener != null) {
            holder.getItemView().setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    itemOnLongClickListener.onLongItem(dataList.get(position), position);
                    return true;
                }
            } );
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position);

    public Context getContext() {
        return context;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
}